import 'dart:convert';

import 'package:equatable/equatable.dart';

class HabitRecord extends Equatable {
  final String habitId;
  final int time;
  final String content;

  const HabitRecord(
      {required this.time, required this.content, required this.habitId});

  @override
  String toString() {
    // TODO: implement toString
    return 'HabitRecord{time: $time, content: $content, habitId: $habitId}';
  }

  static HabitRecord fromJson(Map<String, dynamic> json) {
    return HabitRecord(
        time: json["time"], content: json["content"], habitId: json["habitId"]);
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['habitId'] = habitId;
    data['time'] = time;
    data['content'] = content;
    return data;
  }

  @override
  // TODO: implement props
  List<Object?> get props => [habitId, time, content];

  HabitRecord copyWith(
      {required String habitId,
      required int time,
      required String content,
      required String userId,
      required String objectId}) {
    return HabitRecord(time: time, content: content, habitId: habitId);
  }
}

class Habit extends Equatable {
  ///唯一id uuid v4
  final String id;
  final String name;
  final String iconPath;
  final int mainColor;
  final String mark;

  ///提醒时间，eg：每天 10:20
  ///转化为json存储 ["10:20", "11:50"]
  final List<String> remindTimes;

  ///完成时间
  ///0 任意 1 早上 2 上午 3 中午 4 下午 5 晚上
  final List<int> completeTime;

  ///按周时 完成的day
  ///1---7,周一---周日
  final List<int> completeDays;

  ///0 周期
  ///0 按天
  ///1 按周
  ///2 按月
  final int period;

  ///创建时间
  final int createTime;

  ///修改时间
  final int modifyTime;

  ///是否完成
  final bool completed;

  ///次数
  final int doNum;

  ///记录
  final List<HabitRecord> records;

  const Habit(
      {required this.id,
      required this.name,
      required this.iconPath,
      required this.mainColor,
      required this.mark,
      required this.remindTimes,
      required this.completeTime,
      required this.completeDays,
      required this.period,
      required this.createTime,
      required this.modifyTime,
      required this.completed,
      required this.doNum,
      required this.records});

  @override
  String toString() {
    // TODO: implement toString
    return 'Habit{id: $id, name: $name, iconPath: $iconPath, mainColor: $mainColor, '
        'mark: $mark, remindTimes: $remindTimes, completeTime: $completeTime, '
        'completeDays: $completeDays, period: $period, createTime: $createTime, modifyTime: $modifyTime, '
        'completed: $completed, doNum: $doNum, records: $records}';
  }

  static Habit fromJson(Map<String, dynamic> json,
      {required List<HabitRecord> records}) {
    // var remindTimes = List<String>();
    List<String> remindTimes = [];
    if (json["remindTimes"] != null) {
      var timesJson = jsonDecode(json["remindTimes"]);
      timesJson.forEach((json) {
        remindTimes.add(json.toString());
      });
    }
    // var completeDays = List<int>();
    List<int> completeDays = [];
    if (json["completeDays"] != null) {
      var daysJson = jsonDecode(json["completeDays"]);
      daysJson.forEach((json) {
        completeDays.add(json.toInt());
      });
    }

    return Habit(
        id: json["id"].toString(),
        name: json["name"].toString(),
        iconPath: json["iconPath"].toString(),
        mainColor: json["mainColor"].toInt(),
        mark: json["mark"].toString(),
        remindTimes: remindTimes,
        completeDays: completeDays,
        completeTime: json['completeTime']?.toInt(),
        period: json["period"]?.toInt(),
        createTime: json["createTime"]?.toInt(),
        modifyTime: json["modifyTime"]?.toInt(),
        completed: json["completed"]?.toInt() == 1 ? true : false,
        doNum: json["doNum"].toInt(),
        records: records);
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};

    data["id"] = id;
    data["name"] = name;
    data["iconPath"] = iconPath;
    data["mainColor"] = mainColor;
    data["mark"] = mark;
    //转化为String存储
    List temp = [];
    for (var day in remindTimes) {
      temp.add(day);
    }
    data["remindTimes"] = jsonEncode(temp);
    List temp2 = [];
    for (var day in completeDays) {
      temp2.add(day);
    }
    data["completeDays"] = temp;
    data["completeTime"] = completeTime;
    data["period"] = period;
    data["createTime"] = createTime;
    data["modifyTime"] = modifyTime;
    data["completed"] = completed ? 1 : 0;
    data["doNum"] = doNum;
    return data;
  }

  static Habit createHabit(String key) {
    return Habit(
        id: 'id__$key',
        name: 'name__$key',
        iconPath: 'assets/images/user_icon.jpg',
        mainColor: 122,
        mark: 'mark__$key',
        remindTimes: const ['time1', 'time2'],
        completeTime: const [],
        completeDays: const [],
        period: 1,
        createTime: 1111,
        modifyTime: 0,
        completed: false,
        doNum: 0,
        records: const []);
  }

  @override
  // TODO: implement props
  List<Object?> get props => [
        id,
        name,
        iconPath,
        mainColor,
        mark,
        remindTimes,
        completeTime,
        completeDays,
        period,
        createTime,
        modifyTime,
        completed,
        doNum,
        records
      ];

  Habit copyWith(
      {required String objectId,
      required String name,
      required String iconPath,
      required int mainColor,
      required String mark,
      required List<String> remindTimes,
      required List<int> completeDays,
      required List<int> completeTime,
      required int period,
      required int createTime,
      required int modifyTime,
      required bool completed,
      required int doNum,
      required List<HabitRecord> records,
      required String userId}) {
    return Habit(
        id: id,
        name: name,
        iconPath: iconPath,
        mainColor: mainColor,
        mark: mark,
        remindTimes: remindTimes,
        completeTime: completeTime,
        completeDays: completeDays,
        period: period,
        createTime: createTime,
        modifyTime: modifyTime,
        completed: completed,
        doNum: doNum,
        records: records);
  }
}
